#!/usr/bin/python3

import os
import sys
import time
import urllib.request
from subprocess import check_call, call, DEVNULL

def get_ips():
  urls = ['https://www.cloudflare.com/ips-v4', 'https://www.cloudflare.com/ips-v6']
  ret = []
  for url in urls:
    req = urllib.request.Request(
      url, headers = {'User-Agent': 'curl/7.64.1' },
    )
    ret.extend(urllib.request.urlopen(req).read().decode('utf-8').split())
  return ret

def main(target_file):
  ips = get_ips()
  tmpfile = target_file + '.tmp'
  with open(tmpfile, 'w', encoding='utf-8') as f:
    f.write("# CloudFlare IPs, updated at %s\n" % time.strftime('%Y-%m-%d %H:%M:%S'))
    for ip in ips:
      f.write("set_real_ip_from %s;\n" % ip)
    f.write("real_ip_header CF-Connecting-IP;\n")
  os.rename(tmpfile, target_file)
  if call(['systemctl', 'is-active', 'nginx'], stdout=DEVNULL):
    check_call(['systemctl', 'reload', 'nginx'])

if __name__ == '__main__':
  if len(sys.argv) != 2:
    sys.exit('where to write data?')
  main(sys.argv[1])
